package ink.ykb.pdfdemo.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.EncryptionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.layout.font.FontProvider;

@Component
public class PdfConvertUtil {
  
  @Value("${pdf.user.password}")
  private String userPassword;
  @Value("${pdf.owner.password}")
  private String ownerPassword;
  
	
	public void convertFileByContent(String fontPath,String htmlContent,String pdfPath) {
		ConverterProperties properties = initConverterProperties(fontPath);
	    File pdfFile = new File(pdfPath);
	    try (OutputStream pdfStream = new FileOutputStream(pdfFile);){
	    	PdfDocument pdfDocument = initPdfDocument(fontPath, pdfStream);
	    	
			HtmlConverter.convertToPdf(htmlContent, pdfDocument, properties);
			
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(),e);
		}
	}
	
	public void convertFileByPath(String fontPath,String htmlPath,String pdfPath) {
		ConverterProperties properties = initConverterProperties(fontPath);
		File htmlFile = new File(htmlPath);
		File pdfFile = new File(pdfPath);
		try (FileInputStream htmlStream = new FileInputStream(htmlFile.getAbsolutePath());
		        FileOutputStream pdfStream = new FileOutputStream(pdfFile.getAbsolutePath());){
		  
		    PdfDocument pdfDocument = initPdfDocument(fontPath, pdfStream);
			HtmlConverter.convertToPdf(htmlStream, pdfDocument, properties);
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(),e);
		}
	}
	
	private ConverterProperties initConverterProperties(String fontPath) {
		ConverterProperties properties = new ConverterProperties();
		FontProvider font = new FontProvider();
		font.addFont(fontPath);
		properties.setFontProvider(font);
		return properties;
	}
	
	private PdfDocument initPdfDocument(String fontPath,OutputStream pdfStream) throws IOException {
      PdfFont pdfFont = PdfFontFactory.createFont(
              FontProgramFactory.createFont(StandardFonts.HELVETICA));
      
      WriterProperties properties = new WriterProperties();
      if(userPassword != null && !userPassword.equals("") && ownerPassword != null && !ownerPassword.equals("")) {
        properties.setStandardEncryption(userPassword.getBytes(), ownerPassword.getBytes(), EncryptionConstants.ALLOW_COPY,
            EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA);
      }
      
      PdfWriter pdfWriter = new PdfWriter(pdfStream,properties);
      PdfDocument pdfDocument = new PdfDocument(pdfWriter);
      pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE,
              new WatermarkingEventHandler(pdfFont));
      
      return pdfDocument;
	}

}
